App({
  onLaunch() {
    console.log('小程序启动')
    // 检查本地存储的登录状态
    this.checkLoginStatus()
  },
  onShow() {
  },
  onHide() {
  },
  globalData: {
    userInfo: null,
    isLoggedIn: false
  },
  
  // 检查登录状态
  checkLoginStatus() {
    try {
      const userInfo = wx.getStorageSync('userInfo')
      const isLoggedIn = wx.getStorageSync('isLoggedIn')
      
      if (userInfo && isLoggedIn) {
        this.globalData.userInfo = userInfo
        this.globalData.isLoggedIn = true
      }
    } catch (e) {
      console.error('检查登录状态失败:', e)
    }
  },
  
  // 设置登录状态
  setLoginStatus(userInfo) {
    try {
      this.globalData.userInfo = userInfo
      this.globalData.isLoggedIn = true
      
      // 保存到本地存储
      wx.setStorageSync('userInfo', userInfo)
      wx.setStorageSync('isLoggedIn', true)
      
      console.log('登录状态已设置:', userInfo)
    } catch (e) {
      console.error('设置登录状态失败:', e)
    }
  },
  
  // 清除登录状态
  clearLoginStatus() {
    try {
      this.globalData.userInfo = null
      this.globalData.isLoggedIn = false
      
      // 清除本地存储
      wx.removeStorageSync('userInfo')
      wx.removeStorageSync('isLoggedIn')
      
      console.log('登录状态已清除')
    } catch (e) {
      console.error('清除登录状态失败:', e)
    }
  },
  
  // 获取登录状态
  getLoginStatus() {
    return this.globalData.isLoggedIn
  },
  
  // 获取用户信息
  getUserInfo() {
    return this.globalData.userInfo
  }
}) 